#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
详细调试CM3版本获取问题
"""

import requests
import json

BASE_URL = "http://127.0.0.1:8000"

def debug_cm3_detailed():
    """详细调试CM3版本获取问题"""
    print("=== 详细调试CM3版本获取问题 ===")
    
    # 测试1: 获取固件列表并分析
    print("\n1. 获取固件列表并分析...")
    try:
        response = requests.get(f"{BASE_URL}/ota/list")
        if response.status_code == 200:
            data = response.json()
            print("✅ 固件列表获取成功")
            print(f"总数量: {data['total']}")
            
            cm3_firmwares = []
            dsp_firmwares = []
            
            for item in data['items']:
                print(f"\n固件 ID: {item['id']}")
                print(f"  创建时间: {item['created_at']}")
                print(f"  激活状态: {item['is_active']}")
                
                if item['cm3_version']:
                    print(f"  CM3版本: {item['cm3_version']}")
                    cm3_firmwares.append(item)
                
                if item['dsp_version']:
                    print(f"  DSP版本: {item['dsp_version']}")
                    dsp_firmwares.append(item)
            
            print(f"\nCM3固件数量: {len(cm3_firmwares)}")
            print(f"DSP固件数量: {len(dsp_firmwares)}")
            
            if cm3_firmwares:
                print("\nCM3固件按创建时间排序:")
                sorted_cm3 = sorted(cm3_firmwares, key=lambda x: x['created_at'])
                for firmware in sorted_cm3:
                    print(f"  ID: {firmware['id']}, 版本: {firmware['cm3_version']}, 时间: {firmware['created_at']}")
        else:
            print(f"❌ 获取失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    # 测试2: 直接测试CM3最新版本API
    print("\n2. 测试CM3最新版本API...")
    try:
        response = requests.get(f"{BASE_URL}/ota/latest/cm3")
        if response.status_code == 200:
            data = response.json()
            print("✅ CM3最新版本API成功")
            print(f"返回的CM3版本: {data['data']['version']}")
            print(f"固件ID: {data['data'].get('firmware_id', 'N/A')}")
        else:
            print(f"❌ CM3最新版本API失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")
    
    # 测试3: 测试APP配置接口
    print("\n3. 测试APP配置接口...")
    try:
        response = requests.get(f"{BASE_URL}/v1/app-config")
        if response.status_code == 200:
            data = response.json()
            print("✅ APP配置接口成功")
            print(f"APP配置中的CM3版本: {data['data']['cm3_version']}")
            print(f"APP配置中的Flash版本: {data['data']['flash_version']}")
        else:
            print(f"❌ APP配置接口失败: {response.text}")
    except Exception as e:
        print(f"❌ 请求失败: {e}")

if __name__ == "__main__":
    debug_cm3_detailed() 